//获取应用实例
const app = getApp();
// import trigger from '../../utils/util.js'
Page({
  onLoad: function (options) {
    app.updateFlag(this)
    // (trigger.trigger)(this)  
    var that = this;
    var food_name;
    wx.request({
      url: "https://api.160nurse.com/index.php?c=foods&a=food&food_id=" + options.food_id,
      method: 'get',
      success: function (res) {
        that.setData({
          detail: res.data.data
        });
				wx.setNavigationBarTitle({
						title: res.data.data.food_name 
				});
        wx.setStorageSync('food_name', res.data.data.food_name);
      }
    });
    wx.request({
      url: "https://api.160nurse.com/foods/comments?food_id=" + options.food_id,
      method: 'get',
      success: function (res) {
        that.setData({
          comment: res.data.data.list,
          comment_total: res.data.data.total
        });
      }
    });
  }, 
  // getUserInfo: function () {
  //   var that = this
  //   if (app.getUserInfo(that)) {
  //     that.setData({
  //       user_auth: false
  //     })
  //   }
  // },
  click_on: function () {
    this.setData({
      class: "on"
    })

  },
  onShareAppMessage: function () {
    var nickName = '';
    var avatarUrl = '';
    var food_name = '';
    try {
      nickName = wx.getStorageSync('nickName');
      avatarUrl = wx.getStorageSync('avatarUrl');
      food_name = wx.getStorageSync('food_name');
    } catch (e) {
      // Do something when catch error
    }
    return {
      title: nickName + '向你推荐"' + food_name +'"的功效、营养及搭配宜忌'
    }
     
  },
  click_out: function () {
    this.setData({
      class: " "
    })
  },
  authSuccess(e) {
    this.setData({
      isShow: false
    })
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  click() {
    this.setData({
      isShow: false
    })
  },
  formSubmit: function (e) {
    var nickName = '';
    var avatarUrl = '';
    try {
      nickName = wx.getStorageSync('nickname'); 
      avatarUrl = wx.getStorageSync('icon'); 
    } catch (e) {
      // Do something when catch error
    }
    console.log('昵称', wx.getStorageSync('nickname'), wx.getStorageSync('icon'))
    var that = this;
    if (!app.isAuth){
      that.setData({
        isShow: true
      })
      return
    }
    wx.request({
      url: "https://api.160nurse.com/foods/addcomment",
      method: 'post',
      data: {
        'username': nickName,
        'icon': avatarUrl,
        'content': e.detail.value.content,
        'food_id': e.target.dataset.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.setData({
          comment: res.data.data.list,
          inputTxt:''
        });
      }
    });

  }

})



